<?php

//http://devzone.zend.com/node/view/id/687

class database
{
	private $link;
	private $config;
	
	public $debug = false;
	
	function __construct($server=null, $user=null, $password=null, $dbname=null)
	{
		$this->config = array
		(
			'hostname'=>$server,
			'username'=>$user,
			'password'=>$password,
			'database'=>$dbname
		);
		
		$this->link = new mysqli(
			$this->config['hostname'],
			$this->config['username'],
			$this->config['password'],
			$this->config['database']
		);
		
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
	}
	
	function query($query, $resultmode=null) //todo: lol? ti args exei auto re? :P
	{
		if($this->debug)
		{
			echo '<div class="database-debug"><pre>' . $query . '</pre></div>';
		}
		//todo: debug me plz
		return $this->link->query($query, $resultmode);
	}
	
	function __destruct()
	{
		$this->disconnect();
	}
	
	function disconnect()
	{
		$this->link->close();
	}

	function execute($sql)
	{
		return $this->query($sql);
	}
	
	function numRows($sql)
	{
		$result = $this->query($sql);
		$numRows = $result->numRows();
		$result->close();
		return $numRows;
	}
	
	function numCols($sql)
	{
		$result = $this->query($sql);
		$numCols = $result->field_count();
		$result->close();
		return $numCols;
	}
	
	function getFields($sql)
	{
		$result = $this->query($sql);
		$getFields = $result->fetch_fields();
		$result->close();
		return $getFields;
	}

//	function getObj($sql) //ahm?
//	{
//		$result = $this->query($sql);
//		while($row = $result->fetch_object())
//		{
//			$rows[] = $row;
//		}
//		$result->close();
//		return $rows;
//	}

	function getAll($sql, $limit=null)
	{
		$result = $this->query($sql);
		if(!$result)
		{
			echo $this->link->error;
		}
		while($row = $result->fetch_array(MYSQLI_ASSOC))
		{
			$rows[] = $row;
		}
		$result->close();
		return $rows;
	}

	function getOne($sql)
	{
		$result = $this->query($sql);
		$row = $result->fetch_array(MYSQLI_NUM);
		$result->close();
		return $row[0];
	}
	
	function getRow($sql)
	{
		$result = $this->query($sql);
		$getRow = $result->fetch_array(MYSQLI_ASSOC);
		$result->close();
		return $getRow;
	}
	
	function getCol($sql)
	{
	}
	
	function autoExecute($tableName, $data, $action)
	{
	}
	
	function insert($tableName, $data)
	{
		$query = $this->constructInsertQuery($tableName, $data);
		return $this->query($query);
	}
	
	function update($tableName, $data, $where)
	{
		$query = $this->constructUpdateQuery($tableName, $data, $where);
		$result = $this->query($query);
		if(!$result)
		{
			echo $this->link->error;
		}	
		else
		{
			return $result;	
		}	
	}
	
	function constructInsertQuery($tableName, $data)
	{
		//check if they array is in the correct (k=>v) format
		
		$columns = array_keys($data);

		$query = 'INSERT INTO `'.$tableName.'` ( ';
		$query .= '`'.implode('` , `', $columns).'` )';
		$query .= ' VALUES (';
		foreach ($data as &$value)
		{
			$value = "'{$value}'";
		}
		$query .= implode(', ', $data).');';
		
		return $query;
	}
	
	function constructUpdateQuery($tableName, $data, $where) //conditions instead of where? or even just primary key?
	{
		//check if they array is in the correct (k=>v) format
		
		$pairs = array();
		
		foreach($data as $column=>$value)
		{
			$pairs[] = "`{$column}` = '{$value}'";
		}
		
		$query = "UPDATE `{$tableName}` SET ";
		$query .= implode(',', $pairs);
		$query .= " WHERE {$where};";
		//limit?
		
		return $query;
	}

}
?>